//
//  AgreementViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/23.
//

#import "AgreementViewController.h"
#import "ExceptionsViewController.h"
#import <WebKit/WebKit.h>

@interface AgreementViewController ()<UIScrollViewDelegate,WKNavigationDelegate>
@property(nonatomic,assign) BOOL isLoad;
@property(nonatomic,strong) UIButton *submitBtn;
@end

@implementation AgreementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户协议";
    self.isLoad = false;
}

- (void)initSubviews {
    [super initSubviews];
    
    WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, NavigationContentTop, kScreenWidth, kScreenHeight - NavigationContentTop - 100)];
    wkWebView.scrollView.delegate = self;
    wkWebView.navigationDelegate = self;
    [wkWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com/"]]];
    wkWebView.allowsBackForwardNavigationGestures = NO;
    [self.view addSubview:wkWebView];
    
    MyLinearLayout *bottom = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    bottom.myHorzMargin = 0;
    bottom.myHeight = 100;
    bottom.myBottom = 0;
    bottom.gravity = MyGravity_Horz_Center;
    bottom.subviewVSpace = 10;
    [self.view addSubview:bottom];
    
    {
        UILabel *tishi = [UILabel createLabelText:@"请阅读并滑动到页面底部" font:UIFontMake(13) textColor:HexColor(@"#888888")];
        tishi.myTop = 10;
        tishi.mySize = kAutoSize;
        [bottom addSubview:tishi];
        
        UIButton *submitBtn = [UIButton createButtonText:@"提交" font:UIFontMake(14) textColor:COLOR_4];
        submitBtn.myHorzMargin = 28;
        submitBtn.myHeight = 46;
        ViewCorner(submitBtn, 23);
        [submitBtn az_setGradientBackgroundWithColors:@[HexColor(@"#4BD8B1"),HexColor(@"#2CAED8")] locations:@[@0.0,@1.0] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 1)];
        [submitBtn addTarget:self action:@selector(submitBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [bottom addSubview:submitBtn];
        submitBtn.alpha = 0.5;
        submitBtn.enabled = false;
        self.submitBtn = submitBtn;
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if(self.isLoad){
        if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
            self.submitBtn.enabled = true;
            self.submitBtn.alpha = 1.0;
        } else if (scrollView.contentOffset.y < scrollView.contentSize.height) {
            self.submitBtn.enabled = false;
            self.submitBtn.alpha = 0.5;
        }
    }
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    self.isLoad = YES;
}

- (void)submitBtnClick {
    WeakSelf
    [self alertWithTitle:@"我已经阅读，并了解这是非医疗用途的免责声明。" content:@"" sure:@"确定" handler:^{
        ExceptionsViewController *vc = ExceptionsViewController.new;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
}

@end
